﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 废料_KC : System.Web.UI.Page
    {
        private string state0 = "";
        private string state = "";

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        private void GetState()
        {
            if (this.LeiBie.SelectedItem.Value != "0")
            {
                state += " and t1.废料ID=" + this.LeiBie.SelectedItem.Value;

            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
                state0 += " and t0.日期<='" + d.ToShortDateString() + "'";
            }

            if (this.ShouHuoDanWei.Text != "0" && this.ShouHuoDanWei.Text != "" && this.FenChang.Text != "0")
            {
                state += " and (t1.入出=0 and t1.单位ID=" + this.ShouHuoDanWei.Text
                    + " or t1.入出=1 and t1.单位ID=" + this.FenChang.Text + ")";
            }
            else if (this.ShouHuoDanWei.Text != "0" && this.ShouHuoDanWei.Text != "" && this.FenChang.Text == "0")
            {
                state += " and (t1.入出=0 and t1.单位ID=" + this.ShouHuoDanWei.Text
                    + " or t1.入出=1)";
            }
            else if (this.ShouHuoDanWei.Text == "0" && this.FenChang.Text != "0")
            {
                state += " and (t1.入出=0 or t1.入出=1 and t1.单位ID=" + this.FenChang.Text + ")";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            this.GetState();

            this.SqlText.Text = @"SELECT t1.废料ID,t2.废料名称,t2.规格,t2.单位
,cast(SUM(case when t1.入出=0 or t1.入出=2 then t1.数量 else 0 end) as float) as 入库
,cast(SUM(case when t1.入出=1 or t1.入出=3 then t1.数量 else 0 end) as float) as 出库
,(select top 1 cast(结存数量 as float) from 废料入出库 t0 where t0.废料ID=t1.废料ID " + state0 + @" order by t0.ID desc) as 结存
FROM [废料入出库] t1
left outer join 废料 t2 on t2.ID=t1.废料ID" + state + " group by t1.废料ID,t2.废料名称,t2.规格,t2.单位";

            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.LeiBie.SelectedIndex = 0;
            this.Date1.Text = "";
            this.Date2.Text = "";
        }
    }
}
